package kr.or.kead.module;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class RegJumin extends JPanel {
	private JTextField txtJumin1;
	private JTextField txtJumin2;

	/**
	 * Create the panel.
	 */
	public RegJumin() {
		init();
	}
	
	
	
	private void init() {
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		txtJumin1 = new JTextField();
		txtJumin1.setHorizontalAlignment(SwingConstants.CENTER);
		add(txtJumin1);
		txtJumin1.setColumns(6);		
		txtJumin1.addFocusListener(new Jumin1FocusListener());
		txtJumin1.addKeyListener(new Jumin1KeyListener());
		
		JLabel lblJumin = new JLabel(" - ");
		lblJumin.setAlignmentX(Component.CENTER_ALIGNMENT);
		lblJumin.setAlignmentY(Component.CENTER_ALIGNMENT);
		add(lblJumin);
		
		txtJumin2 = new JTextField();
		txtJumin2.setHorizontalAlignment(SwingConstants.CENTER);		
		txtJumin2.addFocusListener(new Jumin2FocusListener());
		txtJumin2.addKeyListener(new Jumin2KeyListener());
		add(txtJumin2);
		txtJumin2.setColumns(7);
		
	}



	public String getJumin() {
		return txtJumin1.getText() + "-" + txtJumin2.getText();
	}

	
	public void setJumin(String jumin) {
		StringTokenizer st = new StringTokenizer(jumin, "-");
		this.txtJumin1.setText(st.nextToken());
		this.txtJumin2.setText(st.nextToken());
	}

	
	private class Jumin1FocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(txtJumin1.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				txtJumin1.setSelectionStart(0);
				txtJumin1.requestFocus();
			}
		}		
	}
	
	private class Jumin2FocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(txtJumin2.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				txtJumin2.setSelectionStart(0);
				txtJumin2.requestFocus();
			}
		}		
	}
	
	private class Jumin1KeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			int len = txtJumin1.getText().length();
			if (len == 6) {
				txtJumin2.requestFocus();
			}
		}		
	}
	
	private class Jumin2KeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			int len = txtJumin2.getText().length();
			if (len > 7) {
				JOptionPane.showMessageDialog(null, "숫자 7자리만 입력");
				txtJumin2.requestFocus();
			}
		}		
	}
	

}
